//
//  CYTixianVC.m
//  CreditAddressBook
//
//  Created by mosu on 2016/12/14.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYTixianVC.h"
#import "CYUser.h"
#import "MJExtension.h"
#import "UIButton+WebCache.h"
#import "CYLoginHttpTool.h"
#import "DSNavigationBar.h"
#import "UMFeedback.h"
#import "CYJumpsTool.h"
#import "UMSocial.h"
#import "CYMyRelease.h"
#import "CYABTool.h"
#import "MBProgressHUD+MJ.h"
#import "CYHttpTool.h"
#import "CYMeHttpTool.h"
#import "CreditWebViewController.h"
#import "CYCopyViewController.h"
#import "CYRichViewController.h"
#import "CYNavigationVController.h"
#import "CYCloudSyn.h"
#import "UIViewController+Category.h"
#import "CYMessageViewController.h"
#import "CYJsonInfo.h"
#import "CYDiscoveryHttpTool.h"
#import "CYUserAccount.h"
#import "WebViewJavascriptBridge.h"
#import "CYPayViewController.h"
#import "CYAssetManagerVC.h"
#import "LEMDLib.h"
#import "UIImageView+WebCache.h"
#import "CYPersonalDetailsVController.h"
#import "CYTool.h"
#import "CYAssetDetailVC.h"
#import "CYBankcardBandTableViewController.h"
#import "CYGetdealCodeVC.h"
#import "CYPersonReleaseVC.h"
#import "UIInfomationView.h"
#import "CYGFBTool.h"
#import "UIAlertView+Block.h"
#import "CYDES3Util.h"
#import "TOWebViewController+LE.h"
#import "UIAlertView+Block.h"
#import "LEWebViewController+LE.h"
#import "LEUUID.h"
#import "DCPaymentView.h"
#import "CYbankcarddetailVC.h"
#import "JPUSHService.h"
#import "CYShopViewController.h"
#import "CYCardIDCheckVC.h"
#import "CYNewZFBVC.h"
#import "CYMEHelpWebVC.h"
#import "CYSettingTableVC.h"
#import "GCD.h"
#import "CYCreditNewLitmitVC.h"
#import "CYUserCarFindWebVC.h"
#import "CYCreditAddWebVC.h"
#import "CYMoneyAddWebVC.h"
#import "CYTixianVC.h"


@interface CYTixianVC ()<UITextFieldDelegate>

@property(nonatomic, strong) GCDTimer *GCDTimer;
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UITextField *dealCodeTF;
@property (weak, nonatomic) IBOutlet UITextField *yzmTF;
@property (weak, nonatomic) IBOutlet UIButton *yzmBtn;
@property (weak, nonatomic) IBOutlet UILabel *tishiLab;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (nonatomic, strong) NSNumber *account;


@end

@implementation CYTixianVC

- (void)viewDidLoad {
    [super viewDidLoad];
     [self setupNavigationView];
     [self loadBalance];
     
     self.tishiLab.text = @"温馨提示:\n1.手续费：0.5%,单笔最低2元(第三方收取)\n2.提现到账时间：9:00-21:00";
     _submitBtn.layer.cornerRadius = 5.0f;
     
    // Do any additional setup after loading the view from its nib.
}
- (void)setupNavigationView {
     UIView *naviView = [[UIView alloc] init];
     naviView.backgroundColor = CYNaviThemeColor;
     naviView.frame = CGRectMake(0, 0, self.view.width, 64);
     [self.view addSubview:naviView];
}


- (IBAction)yzmClick:(id)sender {
     [self countdown];
     CYUser *user = [CYLoginHttpTool userData].user;
     //确认网络有连接后发送短信验证码
     [CYLoginHttpTool sendSMSWithPhone:user.phone success:^(id json) {
          //CYLog(@"sms: %@", json);
          CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
          if (jsonInfo.isSuccess) {
               [MBProgressHUD showSuccess:jsonInfo.message];
               
               
          }else {
               [MBProgressHUD showError:jsonInfo.message];
          }
     } failure:^(NSError *error) {
          [MBProgressHUD showError:CYServerConnectFailure];
          
     }];
}

/**
 *  倒计时
 */

- (void)countdown{
     self.GCDTimer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
     
     CYWeakObject(weakself, self);
     __block int time = 119;
     [self.GCDTimer event:^{
          if (time < 1) {
               //设置界面的按钮显示 根据自己需求设置
               [_yzmBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
               _yzmBtn.userInteractionEnabled = YES;
               //            btn.backgroundColor = CYColor(79, 118, 245, 1);
               [weakself.GCDTimer destroy];
          } else {
               //        NSLog(@"time:%d", time);
               [_yzmBtn setTitle:[NSString stringWithFormat:@"%ds",time] forState:UIControlStateNormal];
               _yzmBtn.userInteractionEnabled = NO;
               //            btn.backgroundColor = CYColor(211, 211, 211, 1);
          }
          time--;
     } timeInterval:NSEC_PER_SEC * 1];
     [self.GCDTimer start];
}

/**
 *  加载余额
 */
- (void)loadBalance {
     
     CYUser *user = [CYLoginHttpTool userData].user;
     if (user) {
          [CYHttpTool postWithURL:[CYURL shared].getUserAccountBalance params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
               CYJsonInfo *jsoninfo  = [CYJsonInfo objectWithKeyValues:json];
               CYLog(@"jsoninfo = %@",jsoninfo);
               NSDictionary *dic = jsoninfo.data;
               _account = [dic objectForKey:@"balance"];
               
          } failure:^(NSError *error) {
               
          }];
     } else {
          
     }
}



- (IBAction)submitClick:(id)sender {
     if (_accountTF.text.length == 0) {
          CYAlertView(@"请输入借款金额");
          return;
          
     }if (_dealCodeTF.text.length == 0) {
          CYAlertView(@"请输入交易密码");
          return;
     }
     
          double amount = _accountTF.text.doubleValue;
          double yueaccount = _account.doubleValue;
          if (amount>yueaccount) {
               CYAlertView(@"您输入的金额有误");
               return;
          }
               NSString *str = [LEUUID UUIDForVendor];
               NSString *desstr = [CYDES3Util encrypt:_dealCodeTF.text];
               CYUser *user = [CYLoginHttpTool userData].user;
               [CYHttpTool postWithURL:[CYURL shared].withDrawCheckCode params:@{@"userId":user.userId,@"token":user.token,@"dealCode":desstr,@"amount":_accountTF.text,@"machineCode":str,@"phoneCode":_yzmTF.text} success:^(id json) {
                    CYLog(@"json = %@",json);
                    CYJsonInfo *jsonInfo = [CYJsonInfo objectWithKeyValues:json];
                    if (jsonInfo.isSuccess) {
                         [[NSNotificationCenter defaultCenter] postNotificationName:@"CYAssetAccountNotification" object:nil userInfo:nil];
                         CYAlertView(jsonInfo.message);
                         [self.navigationController popViewControllerAnimated:YES];
                         CYAlertView(@"到账时间：工作日6小时之内到账，节假日略有延迟");
                    }else {
                         [MBProgressHUD showError:jsonInfo.message];
                         CYAlertView(jsonInfo.message);
                    }
               } failure:^(NSError *error) {
                    CYLog(@"error = %@",error);
                    CYAlertView(@"服务器连接失败");
               }];
     

}

#pragma mark - 键盘监听
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
     [textField resignFirstResponder];
     return YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     [self.view.window endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
